package com.uw.tookit;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * This class represents a path overlay on the map. It draws the path out on the canvas segment by segment
 * with a magenta transparent paint brush.
 *
 */
public class PathOverlay extends Overlay {
    Road mRoad;
    ArrayList<GeoPoint> mPoints;
    
    /**
     * Constructs a new path overlay with the given road information on the mapview.
     * @param road Road to be drawn.
     * @param mv MapView to be drawn on.
     */
    public PathOverlay(Road road, MapView mv) {
        mRoad = road;
        if (road.mRoute.length > 0) {
            mPoints = new ArrayList<GeoPoint>();
            for (int i = 0; i < road.mRoute.length; i++) {
                mPoints.add(new GeoPoint((int) (road.mRoute[i][1] * 1E6), (int) (road.mRoute[i][0] * 1E6)));
            }
            /*int moveToLat = (mPoints.get(0).getLatitudeE6() + (mPoints.get(
            					mPoints.size() - 1).getLatitudeE6() - mPoints.get(0).getLatitudeE6()) / 2);
            int moveToLong = (mPoints.get(0).getLongitudeE6() + (mPoints.get(
            					mPoints.size() - 1).getLongitudeE6() - mPoints.get(0).getLongitudeE6()) / 2);*/
            int moveToLat = mPoints.get(0).getLatitudeE6();
            int moveToLong = mPoints.get(0).getLongitudeE6();
            GeoPoint moveTo = new GeoPoint(moveToLat, moveToLong);
            MapController mc = mv.getController();
            mc.animateTo(moveTo);
            mc.setZoom(18);
        }
    }
    
    /**
     * Draw call for animated overlays. By default, calls through to draw(Canvas, MapView, boolean) and returns false. 
     * @param canvas The Canvas upon which to draw.
     * @param mapView The MapView that requested the draw.
     * @param shadow If true, draw the shadow layer. If false, draw the overlay contents.
     * @param when The time stamp of the draw. 
     * @return True if you need to be drawn again right away; false otherwise.
     */
    @Override
    public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
        super.draw(canvas, mv, shadow);
        drawPath(mv, canvas);
        return true;
    }
    
    /**
     * Draw the path segment by segment on the canvas.
     * @param mv MapView in which contains the canvas.
     * @param canvas Canvas to draw on.
     */
    public void drawPath(MapView mv, Canvas canvas) {
        int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
        Paint paint = new Paint();
        paint.setColor(Color.MAGENTA);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        paint.setAlpha(70);
        for (int i = 0; i < mPoints.size(); i++) {
            Point point = new Point();
            mv.getProjection().toPixels(mPoints.get(i), point);
            x2 = point.x;
            y2 = point.y;
            if (i > 0) {
                canvas.drawLine(x1, y1, x2, y2, paint);
            }
            x1 = x2;
            y1 = y2;
        }
    }
}
